package com.zretc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zretc.dao.ProductMapper;
import com.zretc.entity.Product;
import com.zretc.service.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements ProductService {

    private static final Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class);

    @Autowired
    private ProductMapper productMapper;

    @Override
    public PageInfo<Product> getBySellerAccount(Map<String, Object> param) {
        Integer pageNum = (Integer) param.get("pageNum");
        Integer pageSize = (Integer) param.get("pageSize");

        // 开始分页
        PageHelper.startPage(pageNum, pageSize);

        List<Product> products = productMapper.getProductBySellerAccount(param);
        logger.info("========="+products);
        // 包装
        PageInfo<Product> pageInfo = new PageInfo<>(products);
        return pageInfo;
    }

    @Override
    public int add(Product product) {
        return productMapper.setProduct(product);
    }

    @Override
    public int delete(Integer productId) {
        return productMapper.deleteProductByProductId(productId);
    }

    @Override
    public int update(Map<String, Object> param) {
        Map<String,Object> map = new HashMap<>();
        if ("".equals(param.get("typeId")) || param.get("typeId") == null) {
            map.put("typeId", null);
        }else {
            map.put("typeId", param.get("typeId"));
        }
        if ("".equals(param.get("productId")) || param.get("productId") == null) {
            map.put("productId", null);
        }else {
            map.put("productId", param.get("productId"));
        }
        if ("".equals(param.get("productName")) || param.get("productName") == null) {
            map.put("productName", null);
        }else {
            map.put("productName", param.get("productName"));
        }
        if ("".equals(param.get("productInventory")) || param.get("productInventory") == null) {
            map.put("productInventory", null);
        }else {
            map.put("productInventory", param.get("productInventory"));
        }
        if ("".equals(param.get("productPrice")) || param.get("productPrice") == null) {
            map.put("productPrice", null);
        }else {
            map.put("productPrice", param.get("productPrice"));
        }
        if ("".equals(param.get("productPicture")) || param.get("productPicture") == null) {
            map.put("productPicture", null);
        }else {
            map.put("productPicture", param.get("productPicture"));
        }
        if ("".equals(param.get("productTime")) || param.get("productTime") == null) {
            map.put("productTime", null);
        }else {
            map.put("productTime", param.get("productTime"));
        }
        if ("".equals(param.get("productStatus")) || param.get("productStatus") == null) {
            map.put("productStatus", null);
        }else {
            map.put("productStatus", param.get("productStatus"));
        }
        if ("".equals(param.get("productIntroduce")) || param.get("productIntroduce") == null) {
            map.put("productIntroduce", null);
        }else {
            map.put("productIntroduce", param.get("productIntroduce"));
        }
        return productMapper.updateProduct(param);
    }

    @Override
    public List<Product> getSellerTypes(String sellerAccount) {
        return productMapper.getTypeBySeller(sellerAccount);
    }

    @Override
    public PageInfo<Product> findProductAll(int pageNum,int pageSize) {

        PageHelper.startPage(pageNum,pageSize);

        List<Product> list = productMapper.getProductAll();

        PageInfo<Product> pageInfo = new PageInfo<>(list);

        return pageInfo;
    }

    @Override
    public PageInfo<Product> findProductLikeName(String productName, int pageNum, int pageSize) {

        PageHelper.startPage(pageNum,pageSize);

        List<Product> list = productMapper.getProductLikeName("%" + productName + "%");

        PageInfo<Product> pageInfo = new PageInfo<>(list);

        return pageInfo;
    }
}
